﻿@{
    Bundle("jquery_validate");
    var optSelected = (Model.Selected as IEnumerable<dynamic>);
    var optAvailable = (Model.Available as IEnumerable<dynamic>)
        .GroupBy(x => JDM.String(x.CodeName));
}

<script type="text/javascript">
    jQuery(document).ready(function () {
        jQuery('@ID("#form")').validate({
            rules: {
                @foreach(var g in optAvailable.Where(x => x.Any(i => JDM.HasFlags(i.Flags, "Required"))))
                {
                    <text>
                    @g.Key: {
                        required: true
                    },
                    </text>
                }
            },
            submitHandler: function(form) {
                var opts = [];
                @foreach(string id in optAvailable.SelectMany(g => g.Select(i => ID("#opt_{0}_{1}", g.Key, JDM.Int32(i.EntityID)))))
                {
                    <text>
                    if (jQuery('@id').prop('selected') || jQuery('@id').prop('checked')) {
                        opts.push(jQuery('@id').val());
                    }
                    </text>
                }
                jQuery('@ID("#opts")').val(opts);
                form.submit();
            }
        });
    });
</script>
<form id="@ID("form")" class="page-form" action="@Url.Action(Loc.Act("MapOption").Localized())" method="get">
    <input type="hidden" name="cartID" value="@Model.CartID" />
    <input id="@ID("opts")" type="hidden" name="options" />
    <ul>
    @foreach(var g in optAvailable)
    {
        bool required = g.Any(x => JDM.HasFlags(x.Flags, "Required"));
        bool multipleChoice = g.Any(x => JDM.HasFlags(x.Flags, "MultipleChoiceAllowed"));
        var selected = optSelected.Where(x => JDM.String(x.CodeName) == g.Key);
        var available = g.Where(x => selected.All(i => JDM.Int32(i.EntityID) != JDM.Int32(x.EntityID)));
        bool selectLayout = g.Count() > 4;
        var t_firstOption = (g.First().Translations as IEnumerable<dynamic>)
            .SingleOrDefault(x => JDM.Int32(x.Language.EntityID) ==
                JDM.Int32(Svc.Localization.Language.EntityID));
        <li>
            <label>@(t_firstOption != null ? JDM.String(t_firstOption.Name) : g.Key)</label>
        </li>
        foreach(var i in selected)
        {
            var t_opt = (i.Translations as IEnumerable<dynamic>)
                .SingleOrDefault(x => JDM.Int32(x.Language.EntityID) ==
                    JDM.Int32(Svc.Localization.Language.EntityID));
            <li>
                <ul class="bar inlinebar">
                    <li><a href="@Url.Action(Loc.Act("UnmapOption").Set(new
                    {
                        cartID = Model.CartID,
                        optionID = JDM.Int32(i.EntityID)
                    }).Localized())">x</a></li>
                </ul>
                @T(Res.Name("Option"),
                    t_opt != null
                        ? JDM.String(t_opt.Value)
                        : String.Empty,
                    Svc.Price((decimal)JDM.Decimal(i.Price)))   
            </li>
        } 
        <li>
        @if(available.Any() && (!selected.Any() || multipleChoice))
        {
            if(selectLayout)
            {
                @:<select name="@g.Key" @(multipleChoice ? "multiple" : String.Empty)>
                if(!required && !multipleChoice)
                {
                    <option value=""></option>
                }
            }
            foreach(var i in available)
            {
                var t_option = (i.Translations as IEnumerable<dynamic>)
                    .SingleOrDefault(x => JDM.Int32(x.Language.EntityID) ==
                        JDM.Int32(Svc.Localization.Language.EntityID));
                int id = JDM.Int32(i.EntityID);
                string html_id = ID("opt_{0}_{1}", g.Key, id);
                bool defaultChoice = JDM.HasFlags(i.Flags, "DefaultChoice");
                string label = T(Res.Name("Option"), 
                    t_option != null
                        ? JDM.String(t_option.Value)
                        : JDM.String(i.CodeValue),
                    Svc.Price((decimal)JDM.Decimal(i.Price)));
                if(selectLayout)
                {
                    <option id="@html_id" value="@id" @(defaultChoice ? "selected" : String.Empty)>@label</option>
                } 
                else
                {
                    <input id="@html_id" name="@g.Key" 
                        type="@(multipleChoice || g.Count() == 1 ? "checkbox" : "radio")" 
                        value="@id" @(defaultChoice ? "checked" : String.Empty) /> @label<br/>
                }
            }  
            if(selectLayout)
            {
                @:</select>
            }
        }
        </li>
    }
    </ul>
    <ul class="bar toolbar">
        <li><button class="button" type="submit">@T(Res.In("Common").Of("Act").Name("Apply"))</button></li>
    </ul>
</form>